
package com.insigma.business.qrcode.common._provider.impl;


import com.insigma.business.qrcode.common.converter.CommonAttachmentProviderConverter;
import com.insigma.business.qrcode.common.dto.*;
import com.insigma.business.qrcode.common.entity.CommonAttachmentDO;
import com.insigma.business.qrcode.common._provider.CommonAttachmentProvider;
import com.insigma.business.qrcode.common.service.CommonAttachmentService;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
@Component("commonAttachmentProvider")
public class CommonAttachmentProviderImpl extends BaseProviderImpl
        implements CommonAttachmentProvider {

    @Autowired
    private CommonAttachmentService commonAttachmentService;


    @Override
    public BasePagingResponse<List<CommonAttachmentDTO>> searchCommonAttachment(final CommonAttachmentSearchRequest searchRequest) {

        final BasePagingResponse<List<CommonAttachmentDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonAttachment的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonAttachmentBSearcher commonAttachmentBSearcher = CommonAttachmentProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CommonAttachmentDO> commonAttachmentDOList = commonAttachmentService.search(commonAttachmentBSearcher);
                final List<CommonAttachmentDTO> commonAttachmentDTOList = CommonAttachmentProviderConverter.convertResponseDOList2DTOList(commonAttachmentDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonAttachmentDTOList, searchRequest,commonAttachmentBSearcher.getTotalCount());
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_ATTACHMENT_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonAttachmentDTO> getCommonAttachment(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonAttachmentDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonAttachment的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CommonAttachmentDO commonAttachmentDO = commonAttachmentService.get((String)baseGetRequest.getId());
                final CommonAttachmentDTO remoteObj = CommonAttachmentProviderConverter.convertResponseDO2DTO(commonAttachmentDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_ATTACHMENT_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonAttachment(final CommonAttachmentInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonAttachment的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final CommonAttachmentDO commonAttachmentDO = CommonAttachmentProviderConverter.convertRequestIO2DO(insertRequest);
                commonAttachmentService.insert(commonAttachmentDO);
                ResponseUtils.setupSuccessResponse(baseResponse, commonAttachmentDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_ATTACHMENT_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonAttachment(final CommonAttachmentUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonAttachment的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CommonAttachmentDO commonAttachmentDO = CommonAttachmentProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = commonAttachmentService.update(commonAttachmentDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_ATTACHMENT_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteCommonAttachment(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonAttachment的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = commonAttachmentService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_ATTACHMENT_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
